PACKAGE=command-not-found
VERSION := $(shell rpm -q --qf '%{version}\n' --specfile $(PACKAGE).spec | head -1)
RELEASE := $(shell rpm -q --qf '%{release}\n' --specfile $(PACKAGE).spec | head -1)
ARCH := $(shell rpm -q --qf '%{arch}\n' --specfile $(PACKAGE).spec | head -1)
ARCHIVEVER := $(VERSION)

TEXTDOMAIN   = ${PACKAGE}

RPM          = /usr/bin/rpm

PRODUCT      = "PCLinuxOS 2007"
export PRODUCT

MSGFMT       = /usr/bin/msgfmt

DESTDIR=

bindir=/usr/bin
sbindir=/usr/sbin
datadir=/usr/share
cachedir=/var/cache
localedir=$(datadir)/locale
libdir=/usr/lib
sysconfdir=/etc


POS := ${wildcard l10n/*/${TEXTDOMAIN}.po}
INSTLOCALES := ${patsubst l10n/%/${TEXTDOMAIN}.po,%,${POS}}
MOS := ${patsubst %.po,%.mo,${POS}}

.PHONY: all clean installdirs install cleandist distdir localcopy tar rpm rpm-install ${INSTLOCALES}

all: ${MOS}

${MOS}: l10n/%/${TEXTDOMAIN}.mo : l10n/%/${TEXTDOMAIN}.po
	$(MSGFMT) -o $@ $<

${INSTLOCALES}: %: l10n/%/${TEXTDOMAIN}.mo
	install -d -m 755 $(DESTDIR)/$(localedir)/$@/LC_MESSAGES/
	install -m 644 $< $(DESTDIR)/$(localedir)/$@/LC_MESSAGES/

installdirs:
	install -d -m 0755 $(DESTDIR)/$(bindir)
	install -d -m 0755 $(DESTDIR)/$(sbindir)
	install -d -m 0755 $(DESTDIR)/$(datadir)/doc/${PACKAGE}
	install -d -m 0755 $(DESTDIR)/$(cachedir)/cnf
	install -d -m 0755 $(DESTDIR)/$(libdir)/cnfbin
	install -d -m 0755 $(DESTDIR)/$(sysconfdir)/profile.d


install: all installdirs ${INSTLOCALES}
	install -m 0755 cnf $(DESTDIR)/$(bindir)/
	install -m 0755 cnf-update $(DESTDIR)/$(sbindir)/
	install -m 0644 README $(DESTDIR)/$(datadir)/doc/${PACKAGE}/
	install -m 0644 COPYING $(DESTDIR)/$(datadir)/doc/${PACKAGE}/
	install -m 0644 cnf.conf $(DESTDIR)/$(sysconfdir)/
	install -m 0755 zzzcnf.sh $(DESTDIR)/$(sysconfdir)/profile.d/

clean:
	rm -f l10n/*/${TEXTDOMAIN}.mo
	rm -f $(PACKAGE)-$(ARCHIVEVER).tar.bz2

cleandist: clean
	rm -Rf $(PACKAGE)-$(VERSION)

distdir:
	if [ ! -d $(PACKAGE)-$(VERSION) ]; then mkdir $(PACKAGE)-$(VERSION); fi

localcopy: clean distdir
	tar cp --exclude=.svn --exclude=$(PACKAGE)-$(VERSION) --exclude=*.tar.bz2 --exclude=*.tar.gz . | tar xp -C $(PACKAGE)-$(VERSION)
	
tar: cleandist localcopy
	tar cjvf $(PACKAGE)-$(ARCHIVEVER).tar.bz2 $(PACKAGE)-$(VERSION)
	rm -rf $(PACKAGE)-$(VERSION)

rpm: tar
	rpmbuild -ta $(PACKAGE)-$(ARCHIVEVER).tar.bz2

rpm-install: rpm
	rpm -Uvh --force /usr/src/rpm/RPMS/$(ARCH)/$(PACKAGE)-$(VERSION)-$(RELEASE).$(ARCH).rpm
